#include "ui.h"
#include "disp.h"
#include "rtc.h"
#include "sht.h"
#include "bitmap.h"
#include "string.h"
#include "pixelwifi.h"
#include "weather.h"
#include "tool.h"

uint8_t ad_enable = 1;
const uint32_t* weather_icon[39] = {
    img_qing_day, img_qing_night, img_qing_day, img_qing_night, img_duoyun, img_duoyun_day, img_duoyun_night, img_duoyun_day, 
    img_duoyun_night, img_yin, img_zhenyu, img_leizhenyu, img_leibingbao, img_xiaoyu, img_zhongyu, img_dayu, 
    img_baoyu, img_dabaoyu, img_tedabaoyu, img_dongyu, img_yujiaxue, img_zhenxue, img_xiaoxue, img_zhongxue, 
    img_daxue, img_baoxue, img_fuchen, img_fuchen, img_fuchen, img_fuchen, img_wu, img_wu, 
    img_feng, img_feng, img_feng, img_feng, img_feng, 
};

static uint32_t font_color;
static uint8_t move_cnt = 0, move_cnt2 = 0;
static int8_t move_y1 = 0;
static uint8_t ui_now = 0;

static void endMove(void)
{
    move_cnt = 0;
    move_cnt2 = 0;
    move_y1 = 0;
}

static uint8_t calStrLength(int8_t a)
{
    if(a>=0 && a<10)
        return 3;
    else if(a<=-10)
        return 11;
    else
        return 7;
    return 0;
}

void weatherTempDisp(void)
{
    uint8_t weight = 0, disp_pos_x = 0;

    char temp_str[30];
    memset(temp_str, 0, 30);
    if(weather_img != NULL)
    {
        clearScreen();
        
        sprintf(temp_str, "%s\"~%s\"", weather_temp_night.c_str(), weather_temp_day.c_str());
        weight = calStrLength(weather_temp_night.toInt());
        weight += calStrLength(weather_temp_day.toInt());
        weight += 11;
        disp_pos_x = (32-weight)/2;

        drawStringGradient(temp_str, disp_pos_x, -6+move_y1, 0x0000FF, 0x00FF00);
        // Serial.print("weather code:");Serial.println(weather_code);
        drawBitmapRGB(weather_icon[weather_code], 2, move_y1, 8, 8);

        memset(temp_str, 0, 30);
        sprintf(temp_str, "%s\"", weather_temp.c_str());

        weight = calStrLength(weather_temp.toInt());
        disp_pos_x = (22-weight)/2;

        if(weather_temp.toInt() < 18)
            drawString(temp_str, 10+disp_pos_x, 2+move_y1, 0x0000C8);
        else if(weather_temp.toInt() >= 18 && weather_temp.toInt() <= 25)
            drawString(temp_str, 10+disp_pos_x, 2+move_y1, 0x00C800);
        else
            drawString(temp_str, 10+disp_pos_x, 2+move_y1, 0xC80000);

        if(move_cnt>100)
        {
            move_y1++;
            if(move_y1 >= 8)
                move_y1 = 8;
        }
        move_cnt++;
    }
}

void timeDisp(void)
{
    static uint16_t cnt = 0, trans_time = 0, trans_n = 0, eye_pos = 0;
    uint8_t i = 0;
    static uint8_t ms = 0;
    char time_str[20];

    clearScreen();
    drawBitmapRGB(img_douren, 1, 1, 8, 6);
    drawPoint(3, 3, 1, 0xC8C8C8);
    drawPoint(4, 3, 1, 0xC8C8C8);
    drawPoint(6, 3, 1, 0xC8C8C8);
    drawPoint(7, 3, 1, 0xC8C8C8);
    drawPoint(3, 5, 1, 0xC8C8C8);
    drawPoint(4, 5, 1, 0xC8C8C8);
    drawPoint(6, 5, 1, 0xC8C8C8);
    drawPoint(7, 5, 1, 0xC8C8C8);
    drawPoint(3, 4, 1, 0xC8C8C8);
    drawPoint(4, 4, 1, 0xC8C8C8);
    drawPoint(6, 4, 1, 0xC8C8C8);
    drawPoint(7, 4, 1, 0xC8C8C8);
    if(cnt >= trans_n)
    {
        cnt = 0;
        eye_pos = random(1, 6);
        trans_time = random(1, 5);
        trans_n = 100 * trans_time;
    }
    switch (eye_pos)
    {
    case 1:
        drawPoint(3, 3, 0, 0);
        drawPoint(6, 3, 0, 0);
        break;
    case 2:
        drawPoint(4, 3, 0, 0);
        drawPoint(7, 3, 0, 0);
        break;
    case 3:
        drawPoint(4, 5, 0, 0);
        drawPoint(7, 5, 0, 0);
        break;
    case 4:
        drawPoint(3, 5, 0, 0);
        drawPoint(6, 5, 0, 0);
        break;        
    case 5:
        drawPoint(3, 4, 0, 0);
        drawPoint(6, 4, 0, 0);
        break;        
    case 6:
        drawPoint(4, 4, 0, 0);
        drawPoint(7, 4, 0, 0);
        break;        
    default:
        break;
    }
    cnt++;

    memset(time_str, 0, 20);
        sprintf(time_str, "%.2d:%.2d", rtc.hour, rtc.min);
    drawStringGradient(time_str, 12, 1, TIME_COLOR_START, TIME_COLOR_END);

    for(i=0; i<7; i++)
    {
        if(rtc.week == i)
        {
            if(WIFI_STATE)
            {
                drawPoint(10+i*3, 6, 1, WIFI_OK_COLOR);
                drawPoint(11+i*3, 6, 1, WIFI_OK_COLOR);
            }
            else
            {
                drawPoint(10+i*3, 6, 1, WIFI_NOT_OK_COLOR);
                drawPoint(11+i*3, 6, 1, WIFI_NOT_OK_COLOR);
            }
        }
        else
        {
            drawPoint(10+i*3, 6, 1, WEEK_COLOR);
            drawPoint(11+i*3, 6, 1, WEEK_COLOR);
        }
    }
}

void dateDisp(void)
{
    char date_str[20];

    clearScreen();
    drawBitmapRGB(rili, 2, 0, 8, 8);
    memset(date_str, 0, 20);
    sprintf(date_str, "%.2d.%.2d", rtc.month, rtc.date);
    drawStringGradient(date_str, 12, 2, DATE_COLOR_START, DATE_COLOR_END);
}

void tempDisp(void)
{
    char temp_str[20];

    clearScreen();
    drawBitmapRGB(wenduji, 2, 0, 5, 8);
    memset(temp_str, 0, 20);
    sprintf(temp_str, "%d.%.2d\"", sht31.temp/100, sht31.temp%100);

    if(sht31.temp<1800)
        drawString(temp_str, 10, 2, 0x0000C8);
    else if(sht31.temp>=1800 && sht31.temp<=2500)
        drawString(temp_str, 10, 2, 0x00C800);
    else
        drawString(temp_str, 10, 2, 0xC80000);
}

void humiDisp(void)
{
    char humi_str[20];

    clearScreen();
    drawBitmapRGB(shuidi, 2, 1, 5, 6);
    memset(humi_str, 0, 20);
    sprintf(humi_str, "%d.%.2d%%", sht31.humi/100, sht31.humi%100);

    if(sht31.humi < 4000)
        drawString(humi_str, 8, 2, 0xC80000);
    else if(sht31.humi>=4000 && sht31.humi<=7000)
        drawString(humi_str, 8, 2, 0x00C800);
    else
        drawString(humi_str, 8, 2, 0x0000C8);
}

static uint8_t transit(void)
{
    static uint8_t cnt = 0;
    static uint8_t bright_decrement = 0, bright_cach = 0;
    uint8_t current_bright = 0;
    current_bright = getCurrentBrightness();
    if(cnt == 0)
    {
        cnt = 1;
        bright_cach = current_bright;
        bright_decrement = current_bright/15;
    }
    current_bright -= bright_decrement;
    if(current_bright > bright_decrement)
    {
        ad_enable = 0;
        setCurrentBrightness(current_bright);
        return current_bright;
    }
    ad_enable = 1;
    setCurrentBrightness(bright_cach);
    cnt = 0;
    return 0;
}

void setFontColor(uint32_t color)
{
    font_color = color;
}

uint32_t getFontColor(void)
{
    return font_color;
}

void uiTask(void)
{
    static uint16_t cnt = 0;
    uint16_t tick = 0;
    static uint16_t _tick = 0;
    static uint8_t menu_cnt = 0;

#if 1
    if(ui_now == 0)
        tick = 1000;
    else if(ui_now==1 && isVaildChar(weather_img.c_str()[0]))
        tick = 160;
    else
        tick = 80 + _tick;
    cnt++;
    if(cnt >= tick)
    {
        _tick = 0;
        ad_enable = 0;
        if(transit() == 0)
        {
            ad_enable = 1;
            cnt = 0;
            ui_now++;
            if(ui_now == 5)
                ui_now = 0;
        }
    }
    switch(ui_now)
    {
    case 0:
        timeDisp();
        break;
    case 1:
        if(isVaildChar(weather_img.c_str()[0]) && WIFI_STATE)
            weatherTempDisp();
        else
            ui_now++;
        break;
    case 2:
        endMove();
        dateDisp();
        break;
    case 3:
        tempDisp();
        break;
    case 4:
        humiDisp();
        break;
    default:
        break;
    }
#else
    clearScreen();
    // Serial.printf("addr1:%p\r\n", weather_icon[1]);
    // Serial.printf("addr2:%p\r\n", img_qing_night);
    drawBitmapRGB(weather_icon[1], 1, 0, 8, 8);
    // drawBitmapRGB(img_zhongxue, 10, 0, 8, 8);
    // drawBitmapRGB(img_daxue, 19, 0, 8, 8);
#endif
    sendToScreen();
}

void wifiDisp(uint8_t state)
{
    static uint8_t n1 = 0, n2 = 0;

    clearScreen();
    drawString("WiFi", 4, 2, 0x0064C8);
    if(state == WIFI_NOT_CONNECT)      //disconnect
    {
        drawChar('X', 19, 2, 5, 0xC80000);
    }
    else if(state == WIFI_CONNECTING)     //connecting
    {
        drawPoint(19, 6, 1, 0x0064C8);
        drawPoint(20, 6, 1, 0x0064C8);
        drawPoint(21, 6, 1, 0x0064C8);
        drawPoint(22, 6, 1, 0x0064C8);
        drawPoint(23, 6, 1, 0x0064C8);
    }
    else if(state == WIFI_CONNECTED)       //connected
    {
        switch (n2)
        {
        case 0:
            drawPoint(19, 4, 1, 0x00C864);
            n2++;
            break;
        case 1:
            drawPoint(19, 4, 1, 0x00C864);
            drawPoint(20, 5, 1, 0x00C864);
            n2++;
            break;
        case 2:
            drawPoint(19, 4, 1, 0x00C864);
            drawPoint(20, 5, 1, 0x00C864);
            drawPoint(21, 6, 1, 0x00C864);
            n2++;
            break;
        case 3:
            drawPoint(19, 4, 1, 0x00C864);
            drawPoint(20, 5, 1, 0x00C864);
            drawPoint(21, 6, 1, 0x00C864);
            drawPoint(22, 5, 1, 0x00C864);
            n2++;
            break;
        case 4:
            drawPoint(19, 4, 1, 0x00C864);
            drawPoint(20, 5, 1, 0x00C864);
            drawPoint(21, 6, 1, 0x00C864);
            drawPoint(22, 5, 1, 0x00C864);
            drawPoint(23, 4, 1, 0x00C864);
            n2++;
            break;
        case 5:
            drawPoint(19, 4, 1, 0x00C864);
            drawPoint(20, 5, 1, 0x00C864);
            drawPoint(21, 6, 1, 0x00C864);
            drawPoint(22, 5, 1, 0x00C864);
            drawPoint(23, 4, 1, 0x00C864);
            drawPoint(24, 3, 1, 0x00C864);
            n2++;
            break;
        case 6:
            drawPoint(19, 4, 1, 0x00C864);
            drawPoint(20, 5, 1, 0x00C864);
            drawPoint(21, 6, 1, 0x00C864);
            drawPoint(22, 5, 1, 0x00C864);
            drawPoint(23, 4, 1, 0x00C864);
            drawPoint(24, 3, 1, 0x00C864);
            drawPoint(25, 2, 1, 0x00C864);
            n2 = 0;
            break;
        default:
            break;
        }
    }
    sendToScreen();
}

void updateDisp(void)
{
    uint8_t i = 0;
    static uint8_t cnt = 0, y = 0;
    clearScreen();
    if(cnt == 20)
    {
        cnt = 0;
        y++;
        if(y == 8)
            y = -7;
    }
    drawBitmapRGB(upload, 12, y, 7, 7);
    for(i=0; i<8; i++)
        drawPoint(12+i, 7, 1, 0x0064C8);
    sendToScreen();
    cnt++;
}
